package gitlet;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static gitlet.Repository.GITLET_DIR_OBJECT_BLOBS;
import static gitlet.Utils.*;


public class Commit implements Serializable {

    private final String message;
    private final String timestamp;
    private final String parent1;
    private final String parent2;
    HashMap<String, String> fileList;

    public Commit(String message, String parent1, String parent2, String timestamp, HashMap<String, String> filesToAdd,
                  ArrayList<String> filesToRemove) {
        this.message = message;
        this.parent1 = parent1;
        this.parent2 = parent2;
        this.timestamp = timestamp;
        if (parent1 == null) {
            this.fileList = new HashMap<String, String>();
        } else {
            Commit parentObj = Commit.fromFile(parent1);
            this.fileList = parentObj.fileList;
            for (HashMap.Entry<String, String> entry : filesToAdd.entrySet()) {
                String key = entry.getKey();
                String value = entry.getValue();
                fileList.put(key, value);
            }
            for (String fileToRm : filesToRemove) {
                fileList.remove(fileToRm);
            }
        }
    }

    //将string sha-1变成Commit obj
    public static Commit fromFile(String filename) {
        File f = new File(Repository.GITLET_DIR_OBJECT, filename);
        return Utils.readObject(f, Commit.class);
    }

    public String getCommitID() {
        byte[] commitByteArr = Utils.serialize(this);
        return sha1(commitByteArr);
    }

    public String getMessage() {

        return this.message;
    }

    public String getParent() {
        return this.parent1;
    }

    public List<String> getParents(){
        List<String> parents  = new ArrayList<>();
        parents.add(this.parent1);
        parents.add(this.parent2);
        return parents;

    }

    public String getParent2(){
        return this.parent2;
    }
    //把commit写进.gitlet/object里面
    public void saveCommit() {
        String location = this.getCommitID();
        File s = new File(Repository.GITLET_DIR_OBJECT, location);
        writeObject(s, this);
    }

    public String getTimeStamp() {
        return this.timestamp;
    }

    public static List<String> findFullIDs(String  prefix){
        List<String> allFile = Utils.plainFilenamesIn(Repository.GITLET_DIR_OBJECT);
        List<String> matchedIDs = new ArrayList<>();
        if (allFile != null) {
            for(String file:allFile){
                if(file.startsWith(prefix)){
                    matchedIDs.add(file);
                }
            }
        }
        return matchedIDs;
    }

    public static String getUniqueIDIfExist(String prefix){
        List<String> here = Commit.findFullIDs(prefix);
        if(here.size() == 1){
            return here.get(0);
        }else{
            return null;
        }
    }
}


